package com.menhai.personmanagement.services;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.menhai.personmanagement.entity.EducationBook;
import com.menhai.personmanagement.entity.EducationLog;
import com.menhai.personmanagement.entity.SysUser;
import com.menhai.personmanagement.entity.User;
import com.menhai.personmanagement.entity.co.UserCo;
import com.menhai.personmanagement.entity.vo.UserVo;
import com.menhai.personmanagement.mapper.EducationBookMapper;
import com.menhai.personmanagement.mapper.EducationLogMapper;
import com.menhai.personmanagement.mapper.SysUserMapper;
import com.menhai.personmanagement.mapper.UserMapper;
import com.menhai.personmanagement.plus.DateUtils;
import com.menhai.personmanagement.plus.Uuid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserService extends Uuid {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private EducationLogMapper educationLogMapper;


    @Autowired
    private EducationBookMapper educationBookMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    public List<User> login(String mobile, String passwrod) {
        return userMapper.login(mobile, passwrod);
    }

    /**
     * @param user
     * @describe 医生列表
     * @Return com.baomidou.mybatisplus.core.metadata.IPage<com.menhai.personmanagement.entity.User>
     * @Date 2021/12/2 13:38
     * @author shikun
     */
    public IPage<User> selectByList(UserCo user) {
        Page<User> page = new Page<>(user.getPageIndex(), user.getPageSize());
        if(user.getHosnumarray()!=null && !user.getHosnumarray().equals("")){
            String ids =  Arrays.asList(user.getHosnumarray().split(",")).stream().map(s -> "'" + s + "'").collect(Collectors.joining(","));
            user.setHosnumarray(ids);
        }
        System.out.println(user.getHosnumarray());
        return userMapper.selectByList(page, user);
    }
    /**
     * @param user
     * @describe 医生列表 不查找已经停用得
     * @Return com.baomidou.mybatisplus.core.metadata.IPage<com.menhai.personmanagement.entity.User>
     * @Date 2021/12/22 16:06
     * @author shikun
     */
    public IPage<User> selectByisAvailable(UserCo user) {
        Page<User> page = new Page<>(user.getPageIndex(), user.getPageSize());
        if(user.getHosnumarray()!=null && !user.getHosnumarray().equals("")){
            String ids =  Arrays.asList(user.getHosnumarray().split(",")).stream().map(s -> "'" + s + "'").collect(Collectors.joining(","));
            user.setHosnumarray(ids);
        }
        return userMapper.selectByisAvailable(page, user);
    }

    /**
     * @param user
     * @describe 医生列表
     * @Return com.baomidou.mybatisplus.core.metadata.IPage<com.menhai.personmanagement.entity.User>
     * @Date 2021/12/2 13:38
     * @author shikun
     */
    public List<UserVo> selectByListExport(UserCo user) {
        return userMapper.selectByListExport(user);
    }


    /**
     * @param user
     * @describe 根据工号查询 医生
     * @Return java.util.List<com.menhai.personmanagement.entity.User>
     * @Date 2022/1/18 11:53
     * @author shikun
     */
    public List<User> selectByjobnumber(User user) {
        QueryWrapper jobnumber = new QueryWrapper<>();
        jobnumber.eq("jobnumber", user.getJobnumber());
        List<User> jobnumberList = userMapper.selectList(jobnumber);
        return jobnumberList;
    }


    /**
     * @param userVo
     * @describe 添加医生
     * @Return int
     * @Date 2021/12/2 13:38
     * @author shikun
     */
    public int saveAddUser(UserVo userVo) {
        QueryWrapper queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("identity", userVo.getIdentity());

        List<User> userList = userMapper.selectList(queryWrapper);
        if (userList.size() > 0) {
            return 2;
        }
        QueryWrapper jobnumber = new QueryWrapper<>();

        jobnumber.eq("jobnumber", userVo.getJobnumber());

        List<User> jobnumberList = userMapper.selectList(jobnumber);
        if (jobnumberList.size() > 0) {
            return 3;
        }
        String ctime = DateUtils.getLocalTime();
        String useruuid = this.getUUId();
        userVo.getEducationBooks().stream().forEach(educationBook -> {
            System.out.println(educationBook);
            educationBook.setCtime(ctime);
            educationBook.setUserid(useruuid);
            educationBook.setProvebookid(this.getUUId());
            educationBookMapper.insert(educationBook);
        });
        userVo.getEducationLogs().stream().forEach(educationLog -> {
            educationLog.setCtime(ctime);
            educationLog.setUserid(useruuid);
            educationLog.setEducationid(this.getUUId());
            educationLogMapper.insert(educationLog);
        });
        userVo.setId(useruuid);
        userVo.setCtime(ctime);
        userVo.setIsAvailable("1");
        userVo.setAudit("0");
        return userMapper.insert(userVo);
    }

    /**
     * @param user
     * @describe 添加医生
     * @Return int
     * @Date 2021/12/15 14:20
     * @author shikun
     */
    public int insertUserlist(User user) {
        return userMapper.insert(user);
    }

    /**
     * @describe 审核医生
     * @param user
     * @Return int
     * @Date 2022/1/19 14:02
     * @author shikun
     */
    public int auditdoctor(User user) {
        return userMapper.updateById(User.builder().id(user.getId()).audit("0").build());
    }


    /**
     * @describe 驳回申请
     * @param user
     * @Return int
     * @Date 2022/1/19 15:48
     * @author shikun
     */
    public int turndown(User user) {
        return userMapper.updateById(User.builder().id(user.getId()).audit("2").build());
    }

    /**
     * @param user
     * @describe 删除医生
     * @Return int
     * @Date 2021/12/2 13:38
     * @author shikun
     */
    public int deleteById(User user) {

        //获取学历list
        List<EducationLog> educationLogs = educationLogMapper.selectByuserid(user.getId());

        //获取学历id list
        List<String> educationLogids = educationLogs.stream().map(EducationLog::getEducationid).collect(Collectors.toList());

        if (educationLogids.size() > 0) {
            //批量删除学历
            educationLogMapper.deleteBatchIds(educationLogids);
        }
        //获取证书list
        List<EducationBook> educationBooks = educationBookMapper.selectByuserid(user.getId());

        //获取证书id list
        List<String> educationbookids = educationBooks.stream().map(EducationBook::getProvebookid).collect(Collectors.toList());

        if (educationbookids.size() > 0) {
            //批量删除证书
            educationBookMapper.deleteBatchIds(educationbookids);
        }
        return userMapper.deleteById(user.getId());
    }

    /**
     * @param user
     * @describe 根据id 查询医生实体
     * @Return com.menhai.personmanagement.entity.User
     * @Date 2021/12/2 13:39
     * @author shikun
     */
    public UserVo selectById(User user) {
        //List<EducationLog> educationLogs =educationLogMapper.selectList(Wrappers.<EducationLog>lambdaQuery().eq(EducationLog::getUserid, user.getId()));
        return userMapper.selectByidlist(user);
    }

    /**
     * @param userCo
     * @describe 修改医生实体
     * @Return int
     * @Date 2021/12/3 16:37
     * @author shikun
     */
    public int updateById(UserCo userCo) {

        QueryWrapper jobnumber = new QueryWrapper<>();
        jobnumber.eq("jobnumber", userCo.getJobnumber());
        List<User> jobnumberList = userMapper.selectList(jobnumber);
        if (jobnumberList.size() > 0) {
            if (jobnumberList.get(0).getId().equals(userCo.getId())) {
                return userMapper.updateById(User.builder().id(userCo.getId()).name(userCo.getName())
                    .mobile(userCo.getMobile()).sex(userCo.getSex()).positionid(userCo.getPositionid())
                    .jobnumber(userCo.getJobnumber()).borntime(userCo.getBorntime()).age(userCo.getAge()).identity(userCo.getIdentity())
                    .address(userCo.getAddress()).nationality(userCo.getNationality()).marriage(userCo.getMarriage()).politicsface(userCo.getPoliticsface()).hometown(userCo.getHometown())
                    .jobtime(userCo.getJobtime()).urgentname(userCo.getUrgentname()).urgentphone(userCo.getUrgentphone()).uptime(DateUtils.getLocalTime())
                    .jobstate("1").classid(userCo.getClassid())
                    .userimgurl(userCo.getUserimgurl()).manageid(userCo.getManageid())
                    .superiorCode(userCo.getSuperiorCode())
                    .superiorName(userCo.getSuperiorName())
                    .build());
            }
            return 2;
        }else{
            return userMapper.updateById(User.builder().id(userCo.getId()).name(userCo.getName())
                .mobile(userCo.getMobile()).sex(userCo.getSex()).positionid(userCo.getPositionid())
                .jobnumber(userCo.getJobnumber()).borntime(userCo.getBorntime()).age(userCo.getAge()).identity(userCo.getIdentity())
                .address(userCo.getAddress()).nationality(userCo.getNationality()).marriage(userCo.getMarriage()).politicsface(userCo.getPoliticsface()).hometown(userCo.getHometown())
                .jobtime(userCo.getJobtime()).urgentname(userCo.getUrgentname()).urgentphone(userCo.getUrgentphone()).uptime(DateUtils.getLocalTime())
                .jobstate("1").classid(userCo.getClassid())
                .userimgurl(userCo.getUserimgurl()).manageid(userCo.getManageid())
                .superiorCode(userCo.getSuperiorCode())
                .superiorName(userCo.getSuperiorName())
                .build());
        }
    }


    /**
     * @param user
     * @describe 停用医生
     * @Return int
     * @Date 2021/12/22 14:15
     * @author shikun
     */
    public int upoByIdisAvailable(User user) {

        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account", user.getJobnumber());
        List<SysUser> sysUserList = sysUserMapper.selectList(queryWrapper);

        sysUserList.forEach(itme -> {
            sysUserMapper.updateById(SysUser.builder().id(itme.getId()).isAvailable(Integer.parseInt(user.getIsAvailable())).build());
        });
        return userMapper.updateById(User.builder().id(user.getId()).isAvailable(user.getIsAvailable()).build());
    }

}
